home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / Spinner.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  12.1 KB  |  464 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Event;
  5. import java.awt.FlowLayout;
  6. import java.awt.LayoutManager;
  7. import java.awt.Panel;
  8. import java.awt.TextField;
  9. import java.awt.Graphics;
  10.  
  11. //     01/29/97    TWB    Integrated changes from Macintosh
  12. //    01/30/97    RKM    Changed updateText to check if the text has changed, to avoid flicker
  13. //                    Also changed paint to first call updateText and removed ifdef MAC
  14.  
  15. /**
  16.  * This abstract class is used to create spinners. A spinner is a component 
  17.  * with two small direction buttons that lets the user scroll a list of 
  18.  * predetermined values to select one, or possibly enter a new legal value.
  19.  * @see symantec.itools.awt.ListSpinner
  20.  * @see symantec.itools.awt.NumericSpinner
  21.  * @version 1.0, Nov 26, 1996
  22.  * @author Symantec
  23.  */
  24.  
  25. public abstract class Spinner
  26.     extends Panel
  27.     implements Orientation
  28. {
  29.     /**
  30.      * The default orientation of the spinner buttons.
  31.      */
  32.     public final int ORIENTATION_DEFAULT = ORIENTATION_VERTICAL;
  33.  
  34.     /**
  35.      * The minimum possible value.
  36.      */
  37.     protected int           min;
  38.     /**
  39.      * The maximum possible value.
  40.      */
  41.     protected int           max;
  42.     /**
  43.      * The current selection value. min <= current <= max.
  44.      * @see #min
  45.      * @see #max
  46.      */
  47.     protected int           current;
  48.     /**
  49.      * The text of the current selection.
  50.      */
  51.     protected String        text;
  52.     /**
  53.      * The width of the optional text field for this spinner.
  54.      */
  55.     protected int           textWidth;
  56.     /**
  57.      * The optional text field for this spinner.
  58.      */
  59.     protected TextField     textField;
  60.     /**
  61.      * Amount the current value chages when scrolling up or down.
  62.      */
  63.     protected int           increment;
  64.     private SpinButtonPanel buttons;
  65.     private boolean         editable;
  66.     private boolean         wrappable;
  67.     private int             orientation;
  68.     private boolean         textFieldAdded;
  69.  
  70.     /**
  71.      * Construct an empty Spinner. It has the default orientation, an increment
  72.      * of 1, no text field, and minimum, maximum, and current values are set to 0.
  73.      */
  74.     protected Spinner()
  75.     {
  76.         textFieldAdded = false;
  77.         increment = 1;
  78.         setWrappable(false);
  79.         setMin(0);
  80.         setMax(0);
  81.         setCurrent(0);
  82.         setOrientation(ORIENTATION_DEFAULT);
  83.         textWidth = -1;
  84.         super.setLayout(new FlowLayout());
  85.     }
  86.  
  87.     /**
  88.      * Conditionally enables editing of the Spinner's TextField.
  89.      * @param f true = allow editing;
  90.      *                  false = disallow editing
  91.      */
  92.     public void setEditable(boolean f)
  93.     {
  94.         editable = f;
  95.  
  96.         if (textFieldAdded)
  97.         {
  98.             setEditable();
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Returns whether the Spinner's TextField is editable.
  104.      * @return whether TextField editable, true = TextField can be edited;
  105.      *                 false = TextField cannot be edited
  106.      */
  107.     public boolean getEditable()
  108.     {
  109.         return editable;
  110.     }
  111.  
  112.     /**
  113.      * Conditionally sets whether the current value wraps around from max
  114.      * to min and from min to max.
  115.      * @param f true if the value can wrap
  116.      * @see #getWrappable
  117.      */
  118.     public void setWrappable(boolean f)
  119.     {
  120.         wrappable = f;
  121.     }
  122.  
  123.     /**
  124.      * Gets whether the current value can wrap around from max
  125.      * to min and from min to max.
  126.      * @return whether wrappable; f true if the value can wrap
  127.      * @see #setWrappable
  128.      */
  129.     public boolean getWrappable()
  130.     {
  131.         return wrappable;
  132.     }
  133.  
  134.     /**
  135.      * Sets the minimum value the spinner may have.
  136.      * @param i the new minimum value
  137.      * @see #getMin
  138.      */
  139.     public void setMin(int i)
  140.     {
  141.         min = i;
  142.     }
  143.  
  144.     /**
  145.      * Gets the current minimum value the spinner may have.
  146.      * @return the current minimum value
  147.      * @see #setMin
  148.      */
  149.     public int getMin()
  150.     {
  151.         return min;
  152.     }
  153.  
  154.     /**
  155.      * Sets the maximum value the spinner may have.
  156.      * @param i the new maximum value
  157.      * @see #getMax
  158.      */
  159.     public void setMax(int i)
  160.     {
  161.         max = i;
  162.     }
  163.  
  164.     /**
  165.      * Gets the current maximum value the spinner may have.
  166.      * @return the current maximum value
  167.      * @see #setMax
  168.      */
  169.     public int getMax()
  170.     {
  171.         return max;
  172.     }
  173.  
  174.     /**
  175.      * Sets the current value of the spinner.
  176.      * @param i the new current value
  177.      * @see #getCurrent
  178.      */
  179.     public void setCurrent(int i)
  180.     {
  181.         current = i;
  182.         updateText();
  183.     }
  184.  
  185.     /**
  186.      * Gets the current value of the spinner.
  187.      * @return the current spinner value
  188.      * @see #setCurrent
  189.      */
  190.     public int getCurrent()
  191.     {
  192.         return current;
  193.     }
  194.  
  195.     /**
  196.      * Sets the orientation of the spin buttons.
  197.      * @param o the new orientation, ORIENTATION_VERTICAL or ORIENTATION_HORIZONTAL
  198.      * @see #getOrientation
  199.      */
  200.     public void setOrientation(int o)
  201.     {
  202.         orientation = o;
  203.  
  204.         if (buttons != null)
  205.         {
  206.             remove(buttons);
  207.         }
  208.  
  209.         switch (orientation)
  210.         {
  211.             case ORIENTATION_VERTICAL :
  212.             {
  213.                 buttons = new VerticalSpinButtonPanel();
  214.                 break;
  215.             }
  216.  
  217.             case ORIENTATION_HORIZONTAL :
  218.             {
  219.                 buttons = new HorizontalSpinButtonPanel();
  220.                 break;
  221.             }
  222.         }
  223.  
  224.         if (textFieldAdded)
  225.         {
  226.             setStyle();
  227.         }
  228.     }
  229.  
  230.     private void setStyle()
  231.     {
  232.         add(buttons);
  233.     }
  234.  
  235.     /**
  236.      * Gets the current orientation of the spin buttons.
  237.      * @return the current orientation, ORIENTATION_VERTICAL or ORIENTATION_HORIZONTAL
  238.      * @see #setOrientation
  239.      */
  240.     public int getOrientation()
  241.     {
  242.         return orientation;
  243.     }
  244.  
  245.     /**
  246.      * Sets whether the spinner buttons will continually send notify messages 
  247.      * while pressed.
  248.      * @param f true = send messages; false = do not send messages
  249.      * @see #getNotifyWhilePressed
  250.      * @see #setDelay
  251.      * @see #getDelay
  252.      */
  253.     public void setNotifyWhilePressed(boolean f)
  254.     {
  255.         buttons.setNotifyWhilePressed(f);
  256.     }
  257.  
  258.     /**
  259.      * Returns the current notifyWhilePressed status.
  260.      * @see #setNotifyWhilePressed
  261.      * @see #setDelay
  262.      * @see #getDelay
  263.      */
  264.     public boolean getNotifyWhilePressed()
  265.     {
  266.         return buttons.getNotifyWhilePressed();
  267.     }
  268.  
  269.     /**
  270.      * Sets the notification message delay of the spinner buttons in milliseconds.
  271.      * @param d the delay between notification messages in milliseconds
  272.      * @see #setNotifyWhilePressed
  273.      * @see #getDelay
  274.      */
  275.     public void setDelay(int d)
  276.     {
  277.         buttons.setDelay(d);
  278.     }
  279.  
  280.     /**
  281.      * Returns the current delay between notification messages of the spinner
  282.      * buttons in milliseconds 
  283.      * @see #setNotifyWhilePressed
  284.      * @see #setDelay
  285.      */
  286.     public int getDelay()
  287.     {
  288.         return buttons.getDelay();
  289.     }
  290.  
  291.     /**
  292.      * Processes events for this component.
  293.      * This is a standard Java AWT method which gets called by the AWT
  294.      * to handle this component's events. The default handler for 
  295.      * components dispatches to one of the following methods as needed:
  296.      * action(), gotFocus(), lostFocus(), keyDown(), keyUp(), mouseEnter(),
  297.      * mouseExit(), mouseMove(), mouseDrag(), mouseDown(), or mouseUp().
  298.      *
  299.      * @param e the event to handle
  300.      * @return true if the event was handled and no further action is needed,
  301.      * false to pass the event to this component's parent
  302.      * @see java.awt.Component#action
  303.      * @see java.awt.Component#gotFocus
  304.      * @see java.awt.Component#lostFocus
  305.      * @see java.awt.Component#keyDown
  306.      * @see java.awt.Component#keyUp
  307.      * @see java.awt.Component#mouseEnter
  308.      * @see java.awt.Component#mouseExit
  309.      * @see java.awt.Component#mouseMove
  310.      * @see java.awt.Component#mouseDrag
  311.      * @see java.awt.Component#mouseDown
  312.      * @see java.awt.Component#mouseUp
  313.      */
  314.     public boolean handleEvent(Event e)
  315.     {
  316.         switch(e.id)
  317.         {
  318.             case Event.SCROLL_PAGE_UP :
  319.             {
  320.                 scrollUp();
  321.                 postEvent(new Event(this, Event.ACTION_EVENT, null));
  322.                 break;
  323.             }
  324.  
  325.             case Event.SCROLL_PAGE_DOWN :
  326.             {
  327.                 scrollDown();
  328.                 postEvent(new Event(this, Event.ACTION_EVENT, null));
  329.                 break;
  330.             }
  331.         }
  332.  
  333.         return super.handleEvent(e);
  334.     }
  335.  
  336.     private void setEditable()
  337.     {
  338.         textField.setEditable(editable);
  339.     }
  340.  
  341.     /**
  342.      * Tells this component that it has been added to a container.
  343.      * This is a standard Java AWT method which gets called by the AWT when 
  344.      * this component is added to a container. Typically, it is used to 
  345.      * create this component's peer.
  346.      *
  347.      * Here it's used to setup the component, creating the TextField as needed.
  348.      * @see java.awt.Container#removeNotify
  349.      */
  350.     public void addNotify()
  351.     {
  352.         super.addNotify();
  353.         if (!textFieldAdded) {
  354.             textField = new TextField(textWidth > 0 ? textWidth - 1 : 10);
  355.             textField.setBackground(Color.white);
  356.             add(textField);
  357.             textFieldAdded = true;
  358.         }
  359.         setEditable();
  360.         setStyle();
  361.         updateText();
  362.     }
  363.     
  364.     /**
  365.      * Paints this component using the given graphics context.
  366.      * This is a standard Java AWT method which typically gets called
  367.      * by the AWT to handle painting this component. It paints this component
  368.      * using the given graphics context. The graphics context clipping region
  369.      * is set to the bounding rectangle of this component and its <0,0>
  370.      * coordinate is this component's top-left corner.
  371.      * 
  372.      * @param g the graphics context used for painting
  373.      * @see java.awt.Component#repaint
  374.      * @see java.awt.Component#update
  375.      */
  376.     public void paint (Graphics g)
  377.     {
  378.         updateText();
  379.         
  380.         super.paint(g);
  381.     }
  382.     
  383.     /**
  384.      * Increments the spinner's value and handles wrapping as needed.
  385.      */
  386.     protected void scrollUp()
  387.     {
  388.         current += increment;
  389.  
  390.         if (current > max)
  391.         {
  392.             if (wrappable)
  393.             {
  394.                 current = min;
  395.             }
  396.             else
  397.             {
  398.                 current = max;
  399.             }
  400.         }
  401.  
  402.         updateText();
  403.     }
  404.  
  405.     /**
  406.      * Decrements the spinner's value and handles wrapping as needed.
  407.      */
  408.     protected void scrollDown()
  409.     {
  410.         current -= increment;
  411.  
  412.         if (current < min)
  413.         {
  414.             if (wrappable)
  415.             {
  416.                 current = max;
  417.             }
  418.             else
  419.             {
  420.                 current = min;
  421.             }
  422.         }
  423.  
  424.         updateText();
  425.     }
  426.  
  427.     /**
  428.      * Updates the text field with the current text, as needed.
  429.      */
  430.     protected void updateText()
  431.     {
  432.         if (textFieldAdded)
  433.         {
  434.             //If the text has changed, put the new text into the text field
  435.             if (!textField.getText().equals(getCurrentText()))
  436.                 textField.setText(getCurrentText());
  437.         }
  438.     }
  439.  
  440.     /**
  441.      * Gets the currently selected String from the list.
  442.      * @return the String currently visible in the Spinner
  443.      */
  444.     protected abstract String getCurrentText();
  445.  
  446.     /**
  447.      * Takes no action. 
  448.      * This is a standard Java AWT method which gets called to specify
  449.      * which layout manager should be used to layout the components in
  450.      * standard containers.
  451.      *
  452.      * Since layout managers CANNOT BE USED with this container the standard
  453.      * setLayout has been OVERRIDDEN for this container and does nothing.
  454.      *
  455.      * @param l the layout manager to use to layout this container's components
  456.      * (IGNORED)
  457.      * @see java.awt.Container#getLayout
  458.      **/
  459.     public void setLayout(LayoutManager lm)
  460.     {
  461.     }
  462. }
  463.  
  464.